package models

import (
	"errors"
	"time"

	"gorm.io/gorm"
)

type Register struct {
	Id             string    `json:"id" gorm:"primaryKey;type:string(50);comment:id"`
	UserName       string    `json:"userName" gorm:"type:varchar(50);comment:user_name"`
	Password       string    `json:"password" gorm:"type:varchar(50);comment:password"`
	Mobile         string    `json:"mobile" gorm:"type:varchar(50);comment:mobile"`
	CreateTimeDts  time.Time `json:"-" gorm:"type:datetime;column:create_time"`
	CreateTime     string    `json:"createTime" gorm:"-"`
	ModifyTimeDts  time.Time `json:"-" gorm:"type:datetime;column:modify_time"`
	ModifyTime     string    `json:"modifyTime" gorm:"-"`
	Alias          string    `json:"alias" gorm:"type:varchar(50);comment:alias"`
	Company        string    `json:"company" gorm:"type:varchar(50);comment:company"`
	Status         string    `json:"status" gorm:"type:varchar(50);comment:status"`
	Email          string    `json:"email" gorm:"type:varchar(50);comment:email"`
	IdentifierType string    `json:"identifierType" gorm:"type:varchar(50);comment:identifier_type"`
}

func (Register) Add(db *gorm.DB, data interface{}) (err error) {
	err = db.Model(&Register{}).Create(data).Error
	return err
}
func (Register) Mod(db *gorm.DB, data interface{}, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	tx := db.Model(&Register{}).Scopes(condition...).Updates(data)
	if tx.RowsAffected == 0 {
		return errors.New("修改失败")
	}
	return tx.Error
}
func (Register) ModByConds(db *gorm.DB, data interface{}, conds string, params ...interface{}) (err error) {
	tx := db.Model(&Register{}).Where(conds, params...).Updates(data)
	if tx.RowsAffected == 0 {
		return errors.New("修改失败")
	}
	return tx.Error
}
func (Register) FindOne(db *gorm.DB, data interface{}, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	//condition = func(db *gorm.DB) *gorm.DB {
	//	return db.Where("Register_name", "二楼机房")
	//}
	err = db.Model(&Register{}).
		Scopes(
			condition...,
		).First(data).Error
	return err
}

func (Register) FindList(db *gorm.DB, data interface{}, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	err = db.Model(&Register{}).Scopes(
		condition...,
	).Find(data).Limit(-1).Offset(-1).Error
	return err
}

func (Register) FindPage(db *gorm.DB, data interface{}, count *int64, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	err = db.Model(&Register{}).Scopes(
		condition...,
	).Find(data).Limit(-1).Offset(-1).Count(count).Error
	return err
}

func (Register) Del(db *gorm.DB, conds string, params ...interface{}) (err error) {
	tx := db.Model(&Register{}).Where(conds, params...).Delete(&Register{})
	if tx.RowsAffected == 0 {
		return errors.New("修改失败")
	}
	return tx.Error
}

func (Register) FindCountByConds(db *gorm.DB, count *int64, conds string, params ...interface{}) {
	db.Model(&Register{}).Where(conds, params...).Find(&Register{}).Count(count)
	return
}
func (Register) FindListByConds(db *gorm.DB, data interface{}, conds string, params ...interface{}) (err error) {
	db.Model(&Register{}).Where(conds, params...).Find(data)
	return
}

func (Register) TableName() string {
	return "t_register"
}

func (e *Register) Generate() interface{} {
	o := *e
	return &o
}
